//
//  LogisticsFormCell.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/6/10.
//  Copyright © 2021 zwb. All rights reserved.
//

import UIKit
import ObjectMapper
import Kingfisher

class ZyaLogisticsFormCell: UITableViewCell, UITextFieldDelegate, operatable {
    
    var textClosure: ((Int, String) -> ())?
    var textSelectClosure: ((Int) -> ())?
    var deleteClosure: (() -> ())?

    private let leftWidth = 80
    
    private(set) lazy var left1: UILabel = {
        return UILabel().then {
            $0.text = "商品名称："
            $0.font = UIFont.systemFont(ofSize: 15)
            $0.textColor = .lightGray
        }
    }()
    
    private(set) lazy var left2: UILabel = {
        return UILabel().then {
            $0.text = "商品规格："
            $0.font = UIFont.systemFont(ofSize: 15)
            $0.textColor = .lightGray
        }
    }()
    
    private(set) lazy var left3: UILabel = {
        return UILabel().then {
            $0.text = "计量单位："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()

    private(set) lazy var left4: UILabel = {
        return UILabel().then {
            $0.text = "数量(不含赠品)："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    
    private(set) lazy var left5: UILabel = {
        return UILabel().then {
            $0.text = "赠送数量："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    private(set) lazy var leftProductDate: UILabel = {
        return UILabel().then {
            $0.text = "生产日期："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    private(set) lazy var left6: UILabel = {
        return UILabel().then {
            $0.text = "有效期至："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    
    private(set) lazy var left7: UILabel = {
        return UILabel().then {
            $0.text = "生产厂商："
            $0.font = UIFont.systemFont(ofSize: 15)
        }
    }()
    
    private(set) lazy var right1: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入品名"
            $0.tag = 1
            $0.textColor = .lightGray
        }
    }()
    
    private(set) lazy var right2: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入规格"
            $0.tag = 2
            $0.textColor = .lightGray
        }
    }()
    
    private(set) lazy var right3: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入计量单位"
            $0.tag = 3
        }
    }()
    
    private(set) lazy var right4: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入数量"
            $0.tag = 4
            $0.keyboardType = .decimalPad
        }
    }()
    
    private(set) lazy var right5: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入赠送数量"
            $0.tag = 5
            $0.keyboardType = .decimalPad
        }
    }()
    private(set) lazy var rightProductDate: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请选择生产日期"
            $0.tag = 66
        }
    }()
    private(set) lazy var right6: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请选择有效期"
            $0.tag = 6
        }
    }()
    
    private(set) lazy var right7: ZyaCardField = {
        return ZyaCardField().then {
            $0.placeholder = "请输入生产厂商"
            $0.tag = 7
        }
    }()
    
    lazy var deleteBtn: UIButton = {
        let v = UIButton()
        v.setImage(UIImage(named:"delete"), for: .normal)
        return v
    }()

    lazy var bg: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 5
        return v
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor(kshex: "#f2f2f2")
        self.contentView.addSubview(bg)
        self.selectionStyle = .none

        bg.addSubview(left1)
        bg.addSubview(left2)
        bg.addSubview(left3)
        bg.addSubview(left4)
        bg.addSubview(left5)
        bg.addSubview(leftProductDate)
        bg.addSubview(left6)
        bg.addSubview(left7)
        
        bg.addSubview(right1)
        bg.addSubview(right2)
        bg.addSubview(right3)
        bg.addSubview(right4)
        bg.addSubview(right5)
        bg.addSubview(rightProductDate)
        bg.addSubview(right6)
        bg.addSubview(right7)
        
        bg.addSubview(deleteBtn)
        
        rightProductDate.delegate = self
        right6.delegate = self
        
        deleteBtn.addTouchUpInSideAction { _ in
            if let clo = self.deleteClosure {
                clo()
            }
        }
        right1.addTextChangeClosure { [weak self] text in
            self?.textClosure?(1, text.text.unwrap)
        }
        
        right2.addTextChangeClosure { [weak self] text in
            self?.textClosure?(2, text.text.unwrap)
        }

        right3.addTextChangeClosure { [weak self] text in
            self?.textClosure?(3, text.text.unwrap)
        }
        right4.addTextChangeClosure { [weak self] text in
            self?.textClosure?(4, text.text.unwrap)
        }
        right5.addTextChangeClosure { [weak self] text in
            self?.textClosure?(5, text.text.unwrap)
        }
        right6.addTextChangeClosure { [weak self] text in
            self?.textClosure?(6, text.text.unwrap)
        }
        right7.addTextChangeClosure { [weak self] text in
            self?.textClosure?(7, text.text.unwrap)
        }
        rightProductDate.addTextChangeClosure { [weak self] text in
            self?.textClosure?(8, text.text.unwrap)
        }
        makeConstraints()
    }
    
    func makeConstraints() {
        bg.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsets(top: 6, left: 6, bottom: 6, right: 6))
        }

        left1.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(6)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        
        left2.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left1.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        
        left3.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left2.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        
        left4.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left3.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(120)
        }
        
        left5.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left4.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        leftProductDate.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left5.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        left6.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(leftProductDate.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }
        
        left7.snp.makeConstraints { (make) in
            make.left.equalTo(left1)
            make.top.equalTo(left6.snp.bottom).offset(2)
            make.height.equalTo(30)
            make.width.equalTo(leftWidth)
        }

        right1.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(35)
            make.centerY.equalTo(left1)
            make.height.equalTo(30)
            make.left.equalTo(left1.snp.right).offset(5)
        }
        
        deleteBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(3)
            make.centerY.equalTo(left1)
            make.height.equalTo(32)
            make.width.equalTo(32)
        }
        
        right2.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left2)
            make.height.equalTo(30)
            make.left.equalTo(left2.snp.right).offset(5)
        }
        
        right3.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left3)
            make.height.equalTo(30)
            make.left.equalTo(left3.snp.right).offset(5)
        }
        
        right4.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left4)
            make.height.equalTo(30)
            make.left.equalTo(left4.snp.right).offset(5)
        }
        
        right5.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left5)
            make.height.equalTo(30)
            make.left.equalTo(left5.snp.right).offset(5)
        }
        rightProductDate.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(leftProductDate)
            make.height.equalTo(30)
            make.left.equalTo(leftProductDate.snp.right).offset(5)
        }
        right6.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left6)
            make.height.equalTo(30)
            make.left.equalTo(left6.snp.right).offset(5)
        }
        
        right7.snp.makeConstraints { (make) in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalTo(left7)
            make.height.equalTo(30)
            make.left.equalTo(left7.snp.right).offset(5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var one: ZyaLogisticsDetail? {
        didSet {
            if let one = one {
                if one.fromContract || self.excuteType == .fix {
                    right1.isUserInteractionEnabled = false
                    right2.isUserInteractionEnabled = false
                    right3.isUserInteractionEnabled = false
                } else {
                    right1.isUserInteractionEnabled = true
                    right2.isUserInteractionEnabled = true
                    right3.isUserInteractionEnabled = true
                }
                right1.text = one.zGoodsNm
                right2.text = one.zSpec
                right3.text = one.zUnit
                right4.text = one.zAmountFh.unwrap
                right5.text = one.zSendAmount.unwrap.isEmpty ? "0" : one.zSendAmount.unwrap
                rightProductDate.text = one.zProduceDate.unwrap
                right6.text = one.zValidDate.unwrap
                right7.text = one.zVender
            }
        }
    }
    
}

extension ZyaLogisticsFormCell {
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if let clo = textSelectClosure {
            clo(textField.tag)
        }
        return false
    }
    
}
 
class ZyaCardField: UITextField {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.textColor = .darkGray
        self.textAlignment = .left
        self.backgroundColor = UIColor(kshex:"#f9f9f9")
        self.font = UIFont.systemFont(ofSize: 15)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

public class ZyaImgMain: Mappable {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    private struct SerializationKeys {
        static let zOrgId = "z_org_id"
        static let fIsMobile = "f_is_mobile"
        static let zUpdateDt = "z_update_dt"
        static let zUpdaterId = "z_updater_id"
        static let fIsPic = "f_is_pic"
        static let zPhoneType = "z_phone_type"
        static let zUpdaterNm = "z_updater_nm"
        static let fPicName = "z_pic_name"
        static let fid = "id_key"
        static let vouId = "vou_id"
        static let fPicUrl = "z_pic_url"
        static let fOrderCd = "f_order_cd"
    }
    
    // MARK: Properties
    public var zOrgId: String?
    public var fIsMobile: Int?
    public var zUpdateDt: String?
    public var zUpdaterId: Int?
    public var fIsPic: Int?
    public var zPhoneType: String?
    public var zUpdaterNm: String?
    public var fPicName: String?
    public var fid: String?
    public var vouId: Int?
    public var fPicUrl: String?
    public var fOrderCd: Int?
    public var z_phone_type = "IOS"
    public var z_type = ""
    public var vou_id = ""
    public var image:UIImage?
    public var sourType:SourType = .image
    
    // MARK: ObjectMapper Initializers
    /// Map a JSON object to this class using ObjectMapper.
    ///
    /// - parameter map: A mapping from ObjectMapper.
    public required init?(map: Map){
        
    }
    
    /// Map a JSON object to this class using ObjectMapper.
    ///
    /// - parameter map: A mapping from ObjectMapper.
    public func mapping(map: Map) {
        zOrgId <- (map[SerializationKeys.zOrgId],StringTransform())
        fIsMobile <- map[SerializationKeys.fIsMobile]
        zUpdateDt <- map[SerializationKeys.zUpdateDt]
        zUpdaterId <- map[SerializationKeys.zUpdaterId]
        fIsPic <- map[SerializationKeys.fIsPic]
        zPhoneType <- map[SerializationKeys.zPhoneType]
        zUpdaterNm <- map[SerializationKeys.zUpdaterNm]
        fPicName <- map[SerializationKeys.fPicName]
        fid <- (map[SerializationKeys.fid],StringTransform())
        vouId <- map[SerializationKeys.vouId]
        fPicUrl <- map[SerializationKeys.fPicUrl]
        fOrderCd <- map[SerializationKeys.fOrderCd]
        
    }
    
    /// Generates description of the object in the form of a NSDictionary.
    ///
    /// - returns: A Key value pair containing all valid values in the object.
    public func dictionaryRepresentation() -> [String: Any] {
        var dictionary: [String: Any] = [:]
        if let value = zOrgId { dictionary[SerializationKeys.zOrgId] = value }
        if let value = fIsMobile { dictionary[SerializationKeys.fIsMobile] = value }
        if let value = zUpdateDt { dictionary[SerializationKeys.zUpdateDt] = value }
        if let value = zUpdaterId { dictionary[SerializationKeys.zUpdaterId] = value }
        if let value = fIsPic { dictionary[SerializationKeys.fIsPic] = value }
        if let value = zUpdaterNm { dictionary[SerializationKeys.zUpdaterNm] = value }
        if let value = fPicName { dictionary[SerializationKeys.fPicName] = value }
        if let value = fid { dictionary[SerializationKeys.fid] = value }
        if let value = vouId { dictionary[SerializationKeys.vouId] = value }
        if let value = fPicUrl { dictionary[SerializationKeys.fPicUrl] = value }
        if let value = fOrderCd { dictionary[SerializationKeys.fOrderCd] = value }
        //        dictionary["z_phone_type"] = z_phone_type
        //        dictionary["z_type"] = z_type
        //        dictionary["vou_id"] = vou_id
        return dictionary
    }
    
}
